#include "../include/Configuration.hpp"
#include "../include/Mylogger.hpp"

#include <fstream>
#include <sstream>
#include <iostream>

using std::make_pair;
using std::ifstream;
using std::stringstream;
using std::cout;
using std::endl;


namespace cdm{

Configuration* Configuration::_conf = nullptr;
Configuration::AutoRelase Configuration::_au;

//获取单例对象
Configuration* Configuration::getConfiguration(const string &path){
    if(_conf == nullptr){
        MutexLock _mutex;
        _mutex.lock();
        _conf = new Configuration(path);
        _mutex.unlock();
    }
    return _conf;
}

//销毁单例对象
void Configuration::destroyCon(){
    if(_conf != nullptr){
        MutexLock _mutex;
        _mutex.lock();
        delete _conf;
        _conf = nullptr;
        _mutex.unlock();
    }
}

Configuration::Configuration(const string &path){
    ifstream ifs(path);
    if(ifs.good()){
        string line;
        //读取配置文件，将内容存储到map中
        while(getline(ifs,line)){
            stringstream iss(line);
            string name,value;
            iss>>name;
            iss>>value;
            _info.insert(make_pair(name,value));
        }
    }else{
        LOG_ERROR("open file failed");
    }
}



}//end of cdm


